import tensorflow as tf

# entropy
print("-" * 30, "entropy", "-" * 30)

x = tf.fill([4], 0.25)
print(x)

entropy = x * tf.math.log(x) / tf.math.log(2.)
print(entropy)

entropy = -tf.reduce_sum(entropy)
print(entropy)

x = tf.constant([0.1, 0.1, 0.1, 0.7])
entropy = -tf.reduce_sum(x * tf.math.log(x) / tf.math.log(2.))
print(entropy)

x = tf.constant([0.01, 0.01, 0.01, 0.97])
entropy = -tf.reduce_sum(x * tf.math.log(x) / tf.math.log(2.))
print(entropy)

x = tf.constant([0.001, 0.001, 0.001, 0.997])
entropy = -tf.reduce_sum(x * tf.math.log(x) / tf.math.log(2.))
print(entropy)

# cross entropy
print("-" * 30, "cross entropy", "-" * 30)

label = [0, 1, 0, 0]
pred = [0.25, 0.25, 0.25, 0.25]
loss = tf.losses.categorical_crossentropy(label, pred)
print(loss)

pred = [0.1, 0.1, 0.8, 0.1]
loss = tf.losses.categorical_crossentropy(label, pred)
print(loss)

pred = [0.01, 0.97, 0.01, 0.01]
loss = tf.losses.categorical_crossentropy(label, pred)
print(loss)

# binary cross entropy
print("-" * 30, "binary cross entropy", "-" * 30)
bc = tf.losses.BinaryCrossentropy()
print(bc([1], [0.1]))
print(tf.losses.binary_crossentropy([1], [0.1]))
